import 'package:flutter/material.dart';
import 'package:top_foundation/component/page_state/widgets/empty.dart';
import 'package:top_foundation/utils/theme/button.dart';
import 'package:top_foundation/utils/theme/theme.dart';

import '../../helper/utils/util.dart';
import 'index.dart';
import 'muti_download_2/index.dart';

class MutiDownloadPage extends GetView<MutiDownloadController> {
  const MutiDownloadPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var list = controller.state.list;
    if (list?.isNotEmpty == true) {
      return ListView.builder(
        itemBuilder: (context, index) {
          var m = controller.state.list![index];
          return InkWell(
            onTap: () {
              if (controller.state.isEdit) {
                controller.onCellChoose(m);
              } else {
                controller.onCell(m);
              }
            },
            child: Container(
              padding: EdgeInsets.all(12.r),
              decoration:
                  BoxDecoration(border: Border(bottom: ITheme.borderSide)),
              child: Row(
                children: [
                  GetBuilder<MutiDownloadController>(
                    id: '${m.name}-choose',
                    builder: (controller) {
                      if (controller.state.isEdit) {
                        bool isChoosed = controller.state.editList.contains(m);

                        return IButton.icon(
                          onPressed: () => controller.onCellChoose(m),
                          icon: Icon(
                            isChoosed
                                ? Icons.check_circle
                                : Icons.circle_outlined,
                            size: 25.r,
                            color: isChoosed
                                ? ITheme.selectedColor
                                : ITheme.borderColor,
                          ),
                          width: 40.r,
                        );
                      }
                      return const SizedBox();
                    },
                  ),
                  Icon(
                    Icons.drive_file_move,
                    size: 40.r,
                    color: Colors.amberAccent,
                  ),
                  SizedBox(width: 10.r),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          m.name,
                          style: ITheme.text.titleMedium,
                        ),
                        Text(
                          '共${m.data?.length}文件',
                          style: ITheme.text.content13,
                        ),
                      ],
                    ),
                  ),
                  Obx(() => Text(
                        m.progress.progress,
                        style: TextStyle(
                            color: ITheme.selectedColor, fontSize: 15.sp),
                      ))
                ],
              ),
            ),
          );
        },
        itemCount: controller.state.list?.length,
      );
    }
    return const EmptyView();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MutiDownloadController>(
      builder: (_) {
        var isEdit = controller.state.isEdit;
        return Scaffold(
          appBar: AppBar(
            title: const Text("下载文件夹"),
            actions: [
              if (controller.state.list?.isNotEmpty == true)
                TextButton(
                    onPressed: () {
                      controller.state.isEdit = !controller.state.isEdit;
                      controller.update();
                    },
                    child: Text(
                      isEdit ? '取消' : '编辑',
                      style: TextStyle(
                        color: !isEdit
                            ? ITheme.selectedColor
                            : ITheme.contentColor,
                        fontSize: 15.sp,
                      ),
                    ))
            ],
          ),
          backgroundColor: Colors.white,
          body: Column(
            children: [
              Expanded(child: _buildView()),
              GetBuilder<MutiDownloadController>(
                id: 'bottom',
                builder: (_) {
                  return Visibility(
                    visible:
                        isEdit && controller.state.list?.isNotEmpty == true,
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 20.r),
                      child: BottomBar(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            GetBuilder<MutiDownloadController>(
                              id: 'all-choose',
                              builder: (_) {
                                var state = controller.state;
                                var isChoosed =
                                    state.editList.length == state.list?.length;
                                return IButton.create(
                                  onPressed: controller.onAllChoose,
                                  child: IButtonContent(
                                    text: '全选',
                                    icon: isChoosed
                                        ? Icons.check_circle
                                        : Icons.circle_outlined,
                                    iconColor: isChoosed
                                        ? ITheme.selectedColor
                                        : ITheme.borderColor,
                                    contentStyle: IButtonContentStyle
                                        .left_icon_right_text,
                                    spacing: 2,
                                  ),
                                );
                              },
                            ),
                            IButton.text(
                              onPressed: controller.onAllRemove,
                              text: '删除',
                              textStyle: TextStyle(
                                color: ITheme.titleColor,
                                fontSize: 15.sp,
                              ),
                              border: Border.all(
                                color: ITheme.borderColor,
                              ),
                              borderRadius: BorderRadius.circular(18.r),
                              height: 36.r,
                              padding: EdgeInsets.symmetric(horizontal: 10.r),
                            )
                          ]),
                    ),
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
